<template>
  <div class="index column">
    <PageHeader :content="pageTitle"></PageHeader>
    <div class="container">
      <!-- <el-button @click="clearFilter">清除所有过滤器</el-button> -->
      <el-table ref="filterTable" :data="tableData" border style="width: 100%">
        <el-table-column prop="groupId" label="项目编号" width="150">
        </el-table-column>
        <el-table-column prop="groupName" label="项目名称" width="120">
        </el-table-column>
        <el-table-column
          label="项目类别"
          width="100"
          :filters="categoryFilter"
          :filter-method="filterCategoryTag"
          filter-placement="bottom-end"
        >
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.category }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="竞赛名称"
          width="120"
          :filters="compNameFilter"
          :filter-method="filterCompNameTag"
          filter-placement="bottom-end"
        >
          <template slot-scope="scope">{{ scope.row.compName }} </template>
        </el-table-column>
        <el-table-column prop="teacherName" label="带队老师" width="120">
        </el-table-column>
        <el-table-column label="商业计划书" width="170">
          <template slot-scope="scope">
            <!-- <div>{{ scope.row.src }}</div> -->
            <el-upload
              style="height: 80px"
              action=""
              :on-preview="handlePreview"
              :file-list="scope.row.src | srcFilter"
            >
            </el-upload>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100"
          :filters="[
            { text: '准备材料', value: 1 },
            { text: '材料审核', value: 2 },
            { text: '审核通过', value: 3 },
          ]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state == 1">准备材料</el-tag>
            <el-tag type="warning" v-else-if="scope.row.state == 2"
              >材料审核</el-tag
            >
            <el-tag type="success" v-else-if="scope.row.state == 3"
              >审核通过</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.state == 2"
              @click="handleClick(scope.row, 3)"
              type="text"
              size="small"
              >同意</el-button
            ><el-button
              v-if="scope.row.state == 2"
              @click="handleClick(scope.row, 1)"
              type="text"
              size="small"
              style="color: red"
              >拒绝</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import compApi from "@/api/competition";
import PageHeader from "@/components/user/PageHeader";
export default {
  name: "examine",
  components: {
    PageHeader,
  },
  data() {
    return {
      pageTitle: "项目审核",
      tableData: [],
      categoryFilter: [],
      compNameFilter: [],
    };
  },
  filters: {
    srcFilter(src) {
      var fileList = [];
      fileList.push({
        name: src.split("/")[src.split("/").length - 1],
        url: src,
      });
      return fileList;
    },
  },
  computed: {
    userInfo() {
      return JSON.parse(localStorage.getItem("userInfo"));
    },
  },
  mounted() {
    this.getGroupData();
  },
  methods: {
    handleClick(row, state) {
      console.log(row);
      var formData = new FormData();
      formData.append("groupId", row.groupId);
      formData.append("state", state);
      compApi.updateGroupState(formData).then((res) => {
        var code = res.data.code;
        if (code == 200) {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.getGroupData();
        }
      });
    },
    getGroupData() {
      var formData = new FormData();
      formData.append("schoolId", this.userInfo.schoolId);
      compApi.getGroupBySchool(formData).then((res) => {
        console.log("getGroupBySchool：", res.data.data);
        var data = res.data.data;
        this.tableData = data;
        data.forEach((element) => {
          var category = element.category;
          if (this.categoryFilter.length == 0) {
            this.categoryFilter.push({ text: category, value: category });
          } else {
            let flag = false;
            this.categoryFilter.forEach((item) => {
              if (category == item.value) {
                flag = true;
              }
            });
            if (!flag) {
              this.categoryFilter.push({
                text: category,
                value: category,
              });
            }
          }
          console.log("竞赛类别赛选完成", this.categoryFilter);
        });
        data.forEach((element) => {
          var compName = element.compName;
          if (this.compNameFilter.length == 0) {
            this.compNameFilter.push({ text: compName, value: compName });
          } else {
            let flag = false;
            this.compNameFilter.forEach((item) => {
              if (compName == item.value) {
                flag = true;
              }
            });
            if (!flag) {
              this.compNameFilter.push({
                text: compName,
                value: compName,
              });
            }
          }
          console.log("竞赛名称赛选完成", this.categoryFilter);
        });
      });
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
      this.$confirm("此操作将下载该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          window.open(file.url, "_blank");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下载",
          });
        });
    },
    clearFilter() {
      this.$refs.filterTable.clearFilter();
    },
    filterTag(value, row) {
      return row.state === value;
    },
    filterCategoryTag(value, row) {
      return row.category === value;
    },
    filterCompNameTag(value, row) {
      return row.compName === value;
    },
  },
};
</script>

<style scoped>
.index {
  width: 1000px;
  box-sizing: border-box;
  padding: 35px 0 0 0;
}
.container {
  margin-top: 35px;
}
</style>